<template>
	<!--pages/register/register.wxml-->
	<view>
		<form @submit="onLogin" @reset="">
			<view class="form">
				<view class="username">
					<label>+86</label>
					<input type="text" placeholder="请输入手机号" name="phoneNumber" />
				</view>
				<view class="pwd">
					<label>密码</label>
					<input type="password" placeholder="请输入密码" name="password" />
				</view>
				<view class="content">
					<text @click="updatePwd">忘记密码？</text>
					<text @click="goRegister">去注册</text>
				</view>
				<button style="width:520rpx" form-type="submit"><text class="text">登 录</text></button>
			</view>
		</form>
	</view>
</template>

<script>
	import {login} from '../../api/index.js'
export default {
	data() {
		return {};
	},
	methods: {
		// 登录
		onLogin: function(e) {
			console.log('123>>>>>>', e.detail.value);
			let user = e.detail.value.phoneNumber;
			let pswd = e.detail.value.password;
			let myreg = /^[1][1,3,2,4,5,7,8,9][0-9]{9}$/;
			if(!myreg.test(user)){
				uni.showModal({
					title:'手机号码输入有误,请核实'
				})
				return
			}
			if(pswd==''){
				if(!myreg.test(user)){
					uni.showModal({
						title:'手机号码输入有误,请核实'
					})
				}
				return
			}
			let data={
				phoneNumber:user,
				password:pswd
			}
			uni.login({
				timeout: 1000,
				success(res) {
					uni.showToast({
						title:'登录中',
						icon:'loading',
						duration:9999
					})
					if (res.code) {
						// 发送请求
						login(data).then(ress=>{
							
							if(ress[1].data.code==200){
								console.log('xxx')
								uni.hideToast();
								let token = ress[1].data.token
								uni.setStorageSync('token',token)
								uni.navigateTo({
									url:"../../pages/index/index",
									fail(e) {
										console.log(e)
									}
								})
							}
							console.log(ress)
							
						}).catch(err=>{
							uni.showToast({
								icon:'none',
								title:'登录超时'
							})
							console.log(err)
						})
						// uni.request({
						// 	method: 'POST',
						// 	url: 'http://192.168.1.14:8086/admin/login',
						// 	data: {
						// 		phoneNumber: user,
						// 		password: pswd
						// 	},
						// 	success(res) {
						// 		console.log('res>>>>>>', res);
						// 		uni.navigateTo({
						// 			url: '/pages/index/index'
						// 		});
						// 		uni.setStorage({
						// 			key:'tokenKey',
						// 			data:res.data.data.token
						// 		});
						// 	}
						// });
					}
				}
			});
		},
		
		//  忘记密码
		updatePwd(){
			uni.navigateTo({
				url: '/pages/updatePwd/updatePwd'
			})
		},
		
		// 注册
		goRegister(){
			uni.navigateTo({
				url:'/pages/goRegister/goRegister'
			})
		}
	}
};
</script>

<style>
@import './register.css';
</style>
